package com.sykj.config;


import com.sykj.api.JavaSmsApi;
import com.sykj.bean.BisBusinessOrderPage;
import com.sykj.bean.BisCustomUserPage;
import com.sykj.bean.bis.BisBusinessOrder;
import com.sykj.bean.bis.BisCustomUser;
import com.sykj.mapper.BisBusinessOrderMapper;
import com.sykj.service.BisBusinessOrderService;
import com.sykj.service.BisCustomUserService;
import lombok.SneakyThrows;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author yanger
 * @description 定时任务一
 * @date 2019/10/16
 */
public class QuartzJob1 extends QuartzJobBean {//继承QuartzJobBean      实现executeInternal
    @Autowired
    private BisBusinessOrderMapper bisBusinessOrderMapper;

    @Autowired
    private BisCustomUserService bisCustomUserService;

    @Autowired
    private BisBusinessOrderService BisBusinessOrderService;


    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取JobDetail中关联的数据
        String msg = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("msg");


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<BisBusinessOrderPage> bisBusinessOrders =  bisBusinessOrderMapper.queryBusinessOut();
        if(!bisBusinessOrders.isEmpty()){
            for (BisBusinessOrderPage b: bisBusinessOrders) {
//                System.out.println(b.getSubscribeTime());//预约时间
//                System.out.println(b.getMobile());//预约电话
//                System.out.println(b.getOrderContent());
                if(b.getSubscribeTime()!=null){

                Date date1 = df.parse(b.getSubscribeTime().toString());
                Date date = new Date();
                // 得到微秒级别的差值
                long diff = date1.getTime() - date.getTime();
                // 将级别提升到分钟
                long days = diff / (1000 * 60);
//                System.out.println(days);
                System.out.println(days);
                if (days >= 0 && days < 1) {
                    System.out.println("---");

//                    System.out.println(b.getMobile());
//                    BisCustomUser user =
//                            bisCustomUserService.getById(b.getCustomUserId());
//                    System.out.println(user.getMobile());
                    BisCustomUserPage bisCustomUserPage = bisCustomUserService.queryById(b.getCustomUserId());
                    System.out.println(b.getMobile());
                JavaSmsApi.sendSms(b.getMobile(),"1234");

                } else if (days < -1) {


//                    System.out.println("---");
                }


             }
            }
        }
        System.out.println("QuartzJob1----" + df.format(new Date()));
    }


}